package cn.yhbk.web.modules.manager.Controller;

import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.common.api.CommonPage;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.common.exception.ApiException;
import cn.yhbk.web.modules.manager.Pojo.DTO.Gpa.GpaDto;
import cn.yhbk.web.modules.manager.Pojo.DTO.Gpa.GpaPagResp;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbStudent;
import cn.yhbk.web.modules.manager.Pojo.VO.GpaRecord.GpaRecordAdd;
import cn.yhbk.web.modules.manager.Pojo.VO.GpaRecord.GpaRecordGet;
import cn.yhbk.web.modules.manager.Pojo.VO.GpaRecord.GpaRecordResP;
import cn.yhbk.web.modules.manager.Service.ITbGpaRecordService;
import cn.yhbk.web.modules.manager.Service.ITbStudentService;
import io.minio.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 绩点记录表 前端控制器
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@RestController
@Api(tags = "绩点管理")
@RequestMapping("/tbGpaRecord")
@Slf4j
public class TbGpaRecordController {

    @Autowired
    ITbGpaRecordService recordService;

    @Autowired
    ITbStudentService studentService;

    @ApiOperation("管理员添加学生的绩点记录")
    @PostMapping("/manage")
    public CommonResult addRecord(@RequestBody @Valid GpaRecordAdd add){
        boolean success = recordService.addRecord(add);
        if(success)return CommonResult.success("添加成功!");

        return CommonResult.failed("添加失败!");
    }

    @ApiOperation("管理员获取学生绩点")
    @GetMapping("/manage")
    public CommonResult getRecord(@RequestParam(name="currentPage",defaultValue = "1")Long currentPage,
                                  @RequestParam(name="pageSize",defaultValue = "5")Long pageSize,
                                  GpaRecordGet condition){
        CommonPage<GpaRecordResP> page = recordService.getRecord(currentPage, pageSize, condition);

        return CommonResult.success(page);
    }

    //学生获取自己的绩点记录
    @ApiOperation("学生获取自己的绩点记录")
    @GetMapping("/student/")
    public CommonResult studentGetRecord(@RequestParam("year") String year,Byte status){
        Long adminId = MyUtil.getCurrentAdminId();
        TbStudent student = studentService.getStudentByAdminId(adminId);
        if(Objects.isNull(student)){
            log.error("student为空");
            throw new ApiException("请求错误，请联系管理员!");
        }

        Long studentId = student.getId();
        GpaRecordGet condition = new GpaRecordGet();
        condition.setYear(year);
        condition.setStuId(studentId);
        condition.setStatus(status);
        CommonPage<GpaRecordResP> resPage = recordService.getRecord(1L,-1L,condition);

        return CommonResult.success(resPage);
    }

    @ApiOperation("删除绩点记录")
    @DeleteMapping("/manage/{recordId}")
    public CommonResult deleteRecord(@PathVariable Long recordId){
        boolean success = recordService.removeById(recordId);
        if(success)return CommonResult.success("删除成功!");

        return CommonResult.failed("删除失败!");
    }

//    @ApiOperation("学生获取绩点排序记录【只有绩点信息，无奖学金信息】")
//    @GetMapping("/getGpaRecordSort")
//    public CommonResult<List<GpaDto>> getGpaRecordSort(){
//        Long adminId = MyUtil.getCurrentAdminId();
//        TbStudent student = studentService.getStudentByAdminId(adminId);
//        List<GpaDto> gpaDtoList = recordService.getGpaRecordSort(student.getId());
//
//        return CommonResult.success(gpaDtoList);
//    }

    @ApiOperation("学工办获取绩点排行")
    @GetMapping("/manage/getGpaSort")
    public CommonResult<GpaPagResp> getGpaSort(@RequestParam(name = "gradeId", required = false) Long gradeId,
                                               @RequestParam(name = "majorId", required = false) Long majorId,
                                               @RequestParam(name = "year", required = false) Integer year,
                                               @RequestParam("currPage") Integer currPage,
                                               @RequestParam("pageSize") Integer pageSize){
        Long adminId = MyUtil.getCurrentAdminId();
        GpaPagResp res = recordService.getGpaSort(adminId, gradeId, majorId, currPage, pageSize, year);

        return CommonResult.success(res);
    }
}
